using System;
using Life.UI.MVC.Extra.Model;
using UnityEngine;

namespace Life.UI.MVC.Extra.Utils
{
    /// <summary>
    /// 工具 快捷构建可等待的 展示UI操作
    /// </summary>
    public class UIShowHandler
    {
        private readonly UIWaiter _pageWaiter = new UIWaiter();
        public PageShowWaitModel Model { get; set; }
        public int Token{ get; set; }
        
        public UIShowHandler(int token)
        {
            Token = token;
        }
        
        /// <summary>
        /// 展示界面
        /// </summary>
        /// <returns></returns>
        public CustomYieldInstruction Execute()
        {
            _pageWaiter.Reset();
            Model ??= new PageShowWaitModel();
            Model.Token = Token;
            Model.ActivateNow = true;
            Model.OnShowEnd = _pageWaiter;
            UIManager.UpdateView(Model);
            return _pageWaiter;
        }
    }
    
    /// <summary>
    /// 工具 快捷构建可等待的 隐藏UI操作
    /// </summary>
    public class UIHideHandler
    {
        private readonly UIWaiter _pageWaiter = new UIWaiter();
        public PageHideWaitModel Model { get; set; }
        public int Token{ get; set; }
        
        public UIHideHandler(int token)
        {
            Token = token;
        }
        
        /// <summary>
        /// 隐藏界面
        /// </summary>
        /// <returns></returns>
        public CustomYieldInstruction Execute()
        {
            _pageWaiter.Reset();
            Model ??= new PageHideWaitModel();
            Model.Token = Token;
            Model.OnHideEnd = _pageWaiter;
            UIManager.UpdateView(Model);
            return _pageWaiter;
        }
    }
    
    /// <summary>
    /// 工具 快捷构建可等待的 展示UI操作
    /// </summary>
    public class UIShowHandler<TModel> where TModel : PageShowWaitModel,new()
    {
        private readonly UIWaiter _pageWaiter = new UIWaiter();
        public TModel Model { get; set; }
        public int Token{ get; set; }
        
        public UIShowHandler(int token)
        {
            Token = token;
        }
        
        /// <summary>
        /// 展示界面
        /// </summary>
        /// <returns></returns>
        public CustomYieldInstruction Execute()
        {
            _pageWaiter.Reset();
            Model ??= new TModel();
            Model.Token = Token;
            Model.ActivateNow = true;
            Model.OnShowEnd = _pageWaiter;
            UIManager.UpdateView(Model);
            return _pageWaiter;
        }
    }
    
    /// <summary>
    /// 工具 快捷构建可等待的 隐藏UI操作
    /// </summary>
    public class UIHideHandler<TModel> where TModel : PageHideWaitModel,new()
    {
        private readonly UIWaiter _pageWaiter = new UIWaiter();
        public TModel Model { get; set; }
        public int Token{ get; set; }
        
        public UIHideHandler(int token)
        {
            Token = token;
        }
        
        /// <summary>
        /// 隐藏界面
        /// </summary>
        /// <returns></returns>
        public CustomYieldInstruction Execute()
        {
            _pageWaiter.Reset();
            Model ??= new TModel();
            Model.Token = Token;
            Model.OnHideEnd = _pageWaiter;
            UIManager.UpdateView(Model);
            return _pageWaiter;
        }
    }
    
    /// <summary>
    /// 工具 快捷构建可等待的 展示UI操作 Waiter可返回参数
    /// </summary>
    public class UIShowHandler<TModel,TModelResult> where TModel : PageShowWaitModel<TModelResult>,new()
    {
        private readonly UIWaiter<TModelResult> _pageWaiter = new UIWaiter<TModelResult>();
        public TModelResult Result => _pageWaiter.Value;
        public TModel Model { get; set; }
        public int Token{ get; set; }
        
        public UIShowHandler(int token)
        {
            Token = token;
        }
        
        /// <summary>
        /// 展示界面
        /// </summary>
        /// <returns></returns>
        public CustomYieldInstruction Execute()
        {
            _pageWaiter.Reset();
            Model ??= new TModel();
            Model.Token = Token;
            Model.ActivateNow = true;
            Model.OnShowEnd = _pageWaiter;
            UIManager.UpdateView(Model);
            return _pageWaiter;
        }
    }
    
    /// <summary>
    /// 工具 快捷构建可等待的 隐藏UI操作 Waiter可返回参数
    /// </summary>
    public class UIHideHandler<TModel,TModelResult> where TModel : PageHideWaitModel<TModelResult>,new()
    {
        private readonly UIWaiter<TModelResult> _pageWaiter = new UIWaiter<TModelResult>();
        public TModelResult Result => _pageWaiter.Value;
        public TModel Model { get; set; }
        public int Token{ get; set; }
        
        public UIHideHandler(int token)
        {
            Token = token;
        }
        
        /// <summary>
        /// 隐藏界面
        /// </summary>
        /// <returns></returns>
        public CustomYieldInstruction Execute()
        {
            _pageWaiter.Reset();
            Model ??= new TModel();
            Model.Token = Token;
            Model.OnHideEnd = _pageWaiter;
            UIManager.UpdateView(Model);
            return _pageWaiter;
        }
    }
}

